package com.itany.nmms.service.proxy;

import com.github.pagehelper.PageInfo;
import com.itany.nmms.entity.Staff;
import com.itany.nmms.exception.CodeErrorException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.ServiceException;
import com.itany.nmms.exception.StaffNotExistException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.StaffService;
import com.itany.nmms.tran.TransactionManager;

/**
 * @author 石小俊
 * @date 2024年04月17日 18:11
 */
public class StaffServiceProxy implements StaffService {
    @Override
    public Staff login(String loginName, String password, String role, String code, String image) throws ServiceException, RequestParameterErrorException, StaffNotExistException, CodeErrorException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        StaffService staffService = (StaffService) ObjectFactory.getObject("staffServiceTarget");
        try {
            tran.begin();
            Staff staff = staffService.login(loginName, password, role, code, image);
            tran.commit();
            return staff;
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            tran.rollback();
            throw e;
        } catch (CodeErrorException e) {
            e.printStackTrace();
            tran.rollback();
            throw e;
        } catch (StaffNotExistException e) {
            e.printStackTrace();
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            e.printStackTrace();
            tran.rollback();
            throw e;
        }
    }

    @Override
    public PageInfo<Staff> findPage(String pageNo, String pageSize, String staffName, String loginName, String phone, String email, String deptId, String role, String isValid) throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        StaffService staffService = (StaffService) ObjectFactory.getObject("staffServiceTarget");
        try {
            tran.begin();
            PageInfo<Staff> staffPage = staffService.findPage(pageNo, pageSize, staffName, loginName, phone, email, deptId, role, isValid);
            tran.commit();
            return staffPage;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }
}
